package com.easyfun.healthmagicbox.inputviews;

import java.sql.SQLException;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.easyfun.healthmagicbox.R;
import com.easyfun.healthmagicbox.base.DatabaseAccess;
import com.easyfun.healthmagicbox.base.DatabaseAccessImplementation;
import com.easyfun.healthmagicbox.base.PicturePickerBaseActivity;
import com.easyfun.healthmagicbox.comm.HealthThreadPool;
import com.easyfun.healthmagicbox.comm.PicturesUploadTask;
import com.easyfun.healthmagicbox.gallary.GridViewActivity.GridImageAdapter;
import com.easyfun.healthmagicbox.ormlite.DatabaseHelper;
import com.easyfun.healthmagicbox.pojo.Picture;
import com.easyfun.healthmagicbox.sync.BasicCommCallback;
import com.easyfun.healthmagicbox.util.PhoneNetworkUtility;
import com.easyfun.healthmagicbox.util.URIConverter;
import com.j256.ormlite.dao.Dao;

public class TakeAPictureActivity  extends PicturePickerBaseActivity implements DatabaseAccess{

	Button takepic;
	private String picFileName = "";
	private ImageView iv_image;
	private static final int SCALE = 8;

	PhoneNetworkUtility pu ;
	private String m_PicTimeStamp;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mytakeapicture);
		iv_image = (ImageView) findViewById(R.id.takepic_img);
		addbackButton(this);
		//personal ID
		if (!checkPersonId()) return;
		
		pu = new PhoneNetworkUtility(this);
		takepic = (Button) findViewById(R.id.btn_pictake);
		takepic.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				m_PicTimeStamp = String.valueOf(System.currentTimeMillis());
				picFileName = getImageFileName(m_personid);
				showPicturePicker(TakeAPictureActivity.this);

			}
		});
		
		Button saveBut = (Button) findViewById(R.id.btn_picsave);
		saveBut.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				EditText picDesc = (EditText) findViewById(R.id.picDesc);
				String desc = picDesc.getText().toString();
				Picture pic = new Picture();
				pic.setCreated(m_PicTimeStamp);
				pic.setPersonID(m_personid);
				pic.setDescription(desc);
				pic.setFilename(picFileName);
				
				Dao<Picture, Integer> picDao;
				try {
					picDao = getHelper(TakeAPictureActivity.this).getPictureDao();
					picDao.create(pic);
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				PicturesUploadTask task = new PicturesUploadTask(TakeAPictureActivity.this
						, new BasicCommCallback(), pic);
				HealthThreadPool.getInstance().execute(task);
				
				if (pu.isWifiNetworkAvailable())
				{
					
					//upload to network storage
				}
				finish();
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		Bitmap newBitmap;
		if (resultCode == RESULT_OK) {

			switch (requestCode) {
			case TAKE_PICTURE:
				// crop
				// startPhotoCrop(TAKE_PHOTE_TEMP_URI,getImageFileName(m_personid));
				// startCropWith3rd(TAKE_PHOTE_TEMP_URI,getImageFileName(m_personid));

				// no crop
				// newBitmap = decodeUriAsBitmap(TAKE_PHOTE_TEMP_URI,SCALE);
				// iv_image.setImageBitmap(newBitmap);
				startCropWith3rd(TAKE_PHOTE_TEMP_URI,picFileName,false,"JPG",640,640);
				break;

			case CHOOSE_PICTURE:
				Uri originalUri = data.getData();
				// crop
				// startPhotoCrop(originalUri,getImageFileName(m_personid));
				// startCropWith3rd(originalUri,getImageFileName(m_personid));

				// no crop
				newBitmap = decodeUriAsBitmap(originalUri, SCALE);
				// iv_image.setImageBitmap(newBitmap);
				URIConverter.savePhotoToAppSDCard(this,newBitmap,
						TAKE_PHOTE_TEMP_FILENAME);// getImageFileName(m_personid)
				startCropWith3rd(TAKE_PHOTE_TEMP_URI,picFileName,false,"JPG",640,640);
				newBitmap.recycle();
				break;

			case CROP_PHOTO_RESULT:
				//
				newBitmap = URIConverter.getPictureFromAppSDCard(this,
						m_personid,picFileName);
				iv_image.setImageBitmap(newBitmap);
				//newBitmap.recycle();
				break;
			default:
				break;
			}
		}
	}
	@Override
	protected String getImageFileName(String m_personID2) {
		String timestamp = m_personID2 + "_"
				+ m_PicTimeStamp+".jpg";
		return timestamp;
	}

	@Override
	public DatabaseHelper getHelper(Context context) {
		return DatabaseAccessImplementation.getInstance().getHelper(this);
	}
}
